<?php

namespace App\Models\OpenShop\ShopCheckAccept;

use App\Models\BaseModel;
use App\Models\OpenShop\InvestmentPromotion\OpenShopMainProcessModel;
use App\Support\ChangeSupport;
use DateTimeInterface;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasOne;

class OpenShopCheckAcceptModel extends BaseModel
{
    use HasFactory;

    protected $table = "open_shop_check_accept";

    protected           $fillable          = [
        'id',
        'mainProcessId',
        'orderNo',
        'step',
        'managerName',
        'managerId',
        'assignTime',
        'formContent',
        'formSubmit',
        'formSubmitTime',
        'provinceManagerConfirmTime',
        'createdAt',
        'createdByUid',
        'updatedAt',
        'updatedByUid',
    ];
    public static array $orderLogTranslate = [
        "formSubmitTime" => [
            "type" => ChangeSupport::FILED_TYPE_DATE_YMD_HIS,
            "desc" => "开始时间"
        ],
        "formSubmit"     => "答题数据"
    ];

    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format("Y-m-d H:i:s");
    }

    /**
     * @param string $orderNo
     * @param array $field
     * @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model|object|null
     */
    public function getInfo(string $orderNo, $field = [])
    {
        return self::query()
                   ->where('orderNo', $orderNo)->select($field)
                   ->first();
    }

    /**
     * 找指派人
     *
     * @param string $orderNo
     * @return int
     */
    public function getAssignUid(string $orderNo)
    {
        return (int)optional(self::getInfo($orderNo, ['managerId']))->managerId;
    }

    public function mainProcessRel(): HasOne
    {
        return $this->hasOne(OpenShopMainProcessModel::class, 'id', 'mainProcessId');
    }
}
